package com.lxt.service;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
/**
 * @author kevin.lv
 * @date 2024/7/10 13:57
 */
@Service
public class OSSService {
    @Autowired
    private OSS ossClient;

    @Autowired
    private String bucketName;

    // 文件上传
    public String uploadFile(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        ossClient.putObject(bucketName, fileName, inputStream);
        return fileName;
    }

    // 文件读取
    public InputStream getFile(String fileName) {
        OSSObject ossObject = ossClient.getObject(bucketName, fileName);
        return ossObject.getObjectContent();
    }
}
